<template>
<div>
  <el-form inline :model="searchForm">
    <el-form-item>
      <el-input v-model="searchForm.username" clearable placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item>
      <el-input v-model="searchForm.nickname" clearable placeholder="昵称"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button @click="getDataList">查询</el-button>
      <el-button type="primary" @click="showDialog()">新增</el-button>
    </el-form-item>
  </el-form>
  <el-table :data="dataList" border>
    <el-table-column prop="userId" label="编号" align="center" header-align="center"/>
    <el-table-column prop="username" label="用户名" align="center" header-align="center"/>
    <el-table-column prop="nickname" label="昵称" align="center" header-align="center"/>
    <el-table-column prop="avatar" label="头像" align="center" header-align="center">
      <template slot-scope="scope">
        <el-image :src="baseImageUrl+scope.row.avatar" style="width: 130px;height: 75px" title="点击查看头像原图" fit="cover" @click="showAvatarDialog(scope.row.avatar)"></el-image>
      </template>
    </el-table-column>
    <el-table-column prop="status" label="状态" align="center" header-align="center">
      <template slot-scope="scope">
        <el-tag v-if="scope.row.status === 0" type="danger" size="small">禁用</el-tag>
        <el-tag v-else size="small">正常</el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="createTime" label="创建时间" align="center" header-align="center"/>
    <el-table-column label="操作" align="center" header-align="center" fixed="right">
      <template slot-scope="scope">
        <el-button size="small" @click="showDialog(scope.row.userId)">修改</el-button>
        <el-button size="small" v-if="scope.row.userId !== 1" @click="deleteInfo(scope.row.userId)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination :current-page="pageIndex" :page-size="pageSize" :total="totalPage" :page-sizes="[10,20,30,50,100,200]" layout="total,sizes,prev,pager,next,jumper" @current-change="currentChange" @size-change="sizeChange"></el-pagination>
  <el-dialog title="查看头像原图" :visible.sync="showAvatar">
    <el-image :src="baseImageUrl+avatarSrc"/>
  </el-dialog>
  <UserAddOrUpdate ref="addOrUpdate" @onCloseWin="getDataList"/>
</div>
</template>

<script>
import UserAddOrUpdate from './UserAddOrUpdate'
export default {
  name: 'User',
  components: {
    UserAddOrUpdate
  },
  data () {
    return {
      searchForm: {
        username: '',
        nickname: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      baseImageUrl: process.env.VUE_APP_BASE_API + '/img/',
      dataListLoading: false,
      showAvatar: false,
      avatarSrc: ''
    }
  },
  methods: {
    getDataList () {
      this.dataListLoading = this.$loading({ text: '加载表格...' })
      this.$http.get('/sys/user/list', {
        page: this.pageIndex,
        limit: this.pageSize,
        ...this.searchForm
      }).then(data => {
        this.dataListLoading.close()
        if (data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        }
      })
    },
    currentChange (val) {
      this.pageIndex = val
      this.getDataList()
    },
    sizeChange (val) {
      this.pageSize = val
      this.getDataList()
    },
    showAvatarDialog (avatarSrc) {
      this.avatarSrc = avatarSrc
      this.showAvatar = true
    },
    showDialog (userId) {
      this.$refs.addOrUpdate.init(userId)
    },
    deleteInfo (userId) {
      this.$confirm(`确定删除编号${userId}的数据吗？`).then(() => {
        this.dataListLoading = this.$loading({ text: '加载表格...' })
        this.$http.delete(`/sys/user/delete/${userId}`).then(data => {
          this.dataListLoading.close()
          if (data.code === 0) {
            this.$message({
              message: '删除成功！',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$alert(data.msg, { type: 'error' }).then(() => {
              this.getDataList()
            })
          }
        })
      })
    }
  },
  created () {
    this.getDataList()
  }
}
</script>

<style scoped>

</style>
